#!/usr/bin/env python
#
# Copyright (C) 2009 Dody Suria Wijaya.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


"""Contains a client to communicate with the Google Ajax Feed servers.
"""


__author__ = 'dodysw@gmail.com (Dody Suria Wijaya)'

import urllib
import ajaxfeed.data
import gdata.client

AJAX_LOAD_FEED_URL = 'http://ajax.googleapis.com/ajax/services/feed/load'
AJAX_FIND_FEED_URL = 'http://ajax.googleapis.com/ajax/services/feed/find'
AJAX_LOOKUP_FEED_URL = 'http://ajax.googleapis.com/ajax/services/feed/lookup'

class AjaxFeedClient(gdata.client.GDClient):
    af_host_language = 'en'
    af_key = '' # google API Key
    af_version = '1.0'

    def __init__(self, *args, **kwargs):
        #~ super(AjaxFeedClient, self).__init__(source = 'ajaxfeed', *args, **kwargs)
        gdata.client.GDClient.__init__(self, source = 'ajaxfeed', *args, **kwargs)

    def _format_standard_url_arg(self, query):
        assert(query != '')
        qs = {'v': self.af_version, 'q': query}
        if self.af_host_language != 'en':
            qs['hl'] = hl
        if self.af_key != '':
            qs['key'] = self.af_key
        return qs

    def set_key(self, key):
        """Set Google AJAX Feed API key.
        Signup at http://code.google.com/apis/ajaxfeeds/key.html
        """
        self.af_key = key

    def set_version(self, version):
        """Set google ajax feed API version. At this moment, only 1.0 is
        available."""
        self.af_version = version

    def set_host_language(self, lang):
        """Indicate to google the language that the client accept."""
        self.af_host_language = lang

    def get_lookup_feed(self, weburl):
        qs = self._format_standard_url_arg(weburl)
        return self.get_feed('%s?%s' % (AJAX_LOOKUP_FEED_URL,
                urllib.urlencode(qs)),
                converter=ajaxfeed.GAjaxFeedLookupFromResponse)

    GetLookupFeed = get_lookup_feed

    def get_find_feed(self, weburl):
        qs = self._format_standard_url_arg(weburl)
        return self.get_feed('%s?%s' % (AJAX_FIND_FEED_URL,
                urllib.urlencode(qs)),
                converter=ajaxfeed.GAjaxFeedFindFromResponse)

    GetFindFeed = get_find_feed

    def get_load_feed(self, weburl, num='', scoring=''):
        qs = self._format_standard_url_arg(weburl)
        if num != '':
            assert(int(num) <= 100)
            qs['num'] = num
        if scoring != '':
            qs['scoring'] = scoring
        return self.get_feed('%s?%s' % (AJAX_LOAD_FEED_URL,
                urllib.urlencode(qs)),
                converter=ajaxfeed.GAjaxFeedLoadFromResponse)

    GetLoadFeed = get_load_feed
